package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.AuthorityService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RequestMapping("/authority")
@RestController
public class AuthorityController {
    @Reference
    private AuthorityService authorityService;

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println(queryPageBean);
        try {
            PageResult page = authorityService.findPage(queryPageBean);
            System.out.println(page.getTotal());
            System.out.println(page.getRows());
            return new Result(true, "查询权限成功", page);
        } catch (Exception e) {
            return new Result(false, "查询权限失败");
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        permission.setDate(new Date());
        System.out.println(permission);
        Result result;
        try {
             result = authorityService.add(permission);
             return result;
        } catch (Exception e) {
            return new Result(false,"添加权限失败");
        }
    }
    @RequestMapping("/delById")
    public Result delById(Integer id){

        try {
            authorityService.delById(id);
            return new Result(true, "删除权限成功");
        }catch (Exception e){
            return new Result(false,"删除权限失败");
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
      try {
          Permission p = authorityService.findById(id);
          return new Result(true,"查询权限成功",p);
      }catch (Exception e){
          return new Result(false,"查询权限失败");
      }
    }
    @RequestMapping("/update")
    public Result update(@RequestBody Permission permission){
      try {
          authorityService.update(permission);
          return new Result(true,"编辑权限成功");
      }catch (Exception e){
          return new Result(false,"编辑权限失败");
      }
    }
}
